import Vuex from "vuex";
import Vue from "vue";
// 使用 vuex 插件
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    age: 18,
    list: [{ age: 18 }, { age: 17 }],
  },
  // 就是一个全局的计算属性
  getters: {
    tenYearsOldAge(state) {
      return state.age + 10;
    },

    getUserByAge(state) {
      return (age) => {
        // state age
        // 匹配
        return state.list.find((user) => user.age === age);
      };
    },
  },
  mutations: {
    setAge(state, payload) {
      console.log(state);
      console.log(payload);
      state.age = payload;
    },

    setTitle() {
      // console.log(state);
      // console.log(payload);
      // state = payload;
    },
  },
  actions: {
    login({ commit }, payload) {
      //   const { username, password } = payload;
      console.log(payload);
      // 模拟的异步请求
      // 请求后端接口
      setTimeout(() => {
        // 赋值
        commit("setAge", 30);
      }, 1000);
    },
  },
});

export default store;
